package com.emm.yixun.website.service.system.log;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.model.ActcommitLog;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.ActcommitLogDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.system.service.ActcommitLogServiceFacade;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
@Service("actCommitLogService")
public class ActCommitLogServiceImp implements ActCommitLogService{

	private final Logger logger = LoggerFactory.getLogger(ActCommitLogServiceImp.class); 
	
	@Autowired
	private ActcommitLogServiceFacade actcommitLogServiceFacade;
	
	@Override
	public ResponseList<ActcommitLog> getActCommitLog(ActcommitLogDto dto) {
		ResponseList<ActcommitLog> resListLog = new ResponseList<ActcommitLog>();
		try {
			resListLog = actcommitLogServiceFacade.findByDto(dto);
		}catch(Exception ex) {
			logger.error("get act commit log error", ex);
		}
		return resListLog;
	}
	
	@Override
	public Response<ActcommitLogDto> saveActCommitLog(ActcommitLogDto dto) {
		Response<ActcommitLogDto> resDto = new Response<ActcommitLogDto>();
		try {
			User user = ShiroUtil.getCurrentUser();
			dto.setCreateUserId(1L);
			if(null != user) {
				dto.setCreateUserId(user.getId());
			}
			resDto = actcommitLogServiceFacade.save(dto);
			return resDto;
		}catch(Exception ex) {
			logger.error("insert act commit log error", ex);
			return resDto;
		}
	}

	/**
	 * 检查审批提交情况
	 */
	@Override
	public boolean checkActCommit(String auditStatus, Long merchantId, Long businessId, String procinstId, String businessType, String businessSource) {
		if(null != auditStatus) {
			//已审核完成的记录,直接返回
			if(AuditStatusEnum.REJECT.getNo().equals(auditStatus)
				|| AuditStatusEnum.SUCCESS.getNo().equals(auditStatus)) {
				return false;
			}
		}
		ActcommitLogDto actCommitLogDto = new ActcommitLogDto();
		actCommitLogDto.setMerchantId(merchantId);
		actCommitLogDto.setBusinessId(businessId);
		actCommitLogDto.setActProcinst(procinstId);
		ResponseList<ActcommitLog> resList = getActCommitLog(actCommitLogDto);
		if(resList.isSuccess()) {
			List<ActcommitLog> logList = resList.getEntity();
			if(null == logList || logList.size() == 0) {
				actCommitLogDto.setBusinessType(businessType);
				actCommitLogDto.setBusinessSource(businessSource);
				actCommitLogDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
				saveActCommitLog(actCommitLogDto);
				return true;
			}else {
				//提交过记录,直接返回,不能往下执行
				return false;
			}
		}else {
			return true;
		}
	}


}
